﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCG.General
{
    public enum SCGDirection { Up, Right, Down, Left };
    public enum SCGDirection8 { Up, UpRight, Right, DownRight, Down, DownLeft, Left, UpLeft };
    public enum SCGOrientation { Horizontal, Vertical };

    public static class DirectionUtils
    {
        public static SCGDirection RandomDirection
        {
            get
            {
                int n = _rnd.Next(4);
                return (SCGDirection)n;
            }
        }
        public static SCGDirection8 RandomDirection8
        {
            get
            {
                int n = _rnd.Next(8);
                return (SCGDirection8)n;
            }
        }

        public static SCGDirection TurnRight(this SCGDirection dir)
        {
            if (dir == SCGDirection.Left)
                return SCGDirection.Up;
            else
                return dir + 1;
        }
        public static SCGDirection TurnLeft(this SCGDirection dir)
        {
            if (dir == SCGDirection.Up)
                return SCGDirection.Left;
            else
                return dir - 1;
        }

        public static SCGDirection8 TurnRight(this SCGDirection8 dir)
        {
            if (dir == SCGDirection8.UpLeft)
                return SCGDirection8.Up;
            else
                return dir + 1;
        }
        public static SCGDirection8 TurnLeft(this SCGDirection8 dir)
        {
            if (dir == SCGDirection8.Up)
                return SCGDirection8.UpLeft;
            else
                return dir - 1;
        }

        public static void Advance(this SCGDirection dir, ref int x, ref int y)
        {
            switch (dir)
            {
                case SCGDirection.Up:
                    y--;
                    break;
                case SCGDirection.Right:
                    x++;
                    break;
                case SCGDirection.Down:
                    y++;
                    break;
                case SCGDirection.Left:
                    x--;
                    break;
            }
        }
        public static void Advance(this SCGDirection8 dir, ref int x, ref int y)
        {
            switch (dir)
            {
                case SCGDirection8.Up:
                    y--;
                    break;
                case SCGDirection8.UpRight:
                    y--;
                    x++;
                    break;
                case SCGDirection8.Right:
                    x++;
                    break;
                case SCGDirection8.DownRight:
                    x++;
                    y++;
                    break;
                case SCGDirection8.Down:
                    y++;
                    break;
                case SCGDirection8.DownLeft:
                    y++;
                    x--;
                    break;
                case SCGDirection8.Left:
                    x--;
                    break;
                case SCGDirection8.UpLeft:
                    y--;
                    x--;
                    break;
            }
        }

        private static Random _rnd = new Random();
    }
}
